//
//  MouseEventView.swift
//  iDev
//
//  Created by lbxia on 2021/8/30.
//

import Cocoa


open class MouseEventView: FlippedView {

    public enum MouseEventUseCase {
        case enteredAndExited
        case moved
    }
    
    private var trackingEnteredAndExited: NSTrackingArea?
    private var trackingMoved: NSTrackingArea?
    
 
    open func mouseLeftClick(){
        
    }
    open func mouseLeftClickUp(){
        
    }
    open func mouseRightClick(){
        
    }
    open func mouseRightClickUp(){
        
    }
    
    open func mouseMoveEntered(){
        
    }
    
    open func mouseMoveExited(){
        
    }
    
    //MARK: -   mouse tracking area need active
    
    open func addMonitorWithCase(_ useCase: MouseEventUseCase ){
        
        switch useCase {
        case .enteredAndExited:
            
            let options: NSTrackingArea.Options =  NSTrackingArea.Options(rawValue: NSTrackingArea.Options.mouseEnteredAndExited.rawValue | NSTrackingArea.Options.activeInKeyWindow.rawValue )

            let area = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
            self.addTrackingArea(area)
            
            self.trackingEnteredAndExited = area
            
        case .moved:
            
            let options: NSTrackingArea.Options =  NSTrackingArea.Options(rawValue:  NSTrackingArea.Options.activeInKeyWindow.rawValue | NSTrackingArea.Options.mouseMoved.rawValue)

            let area = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)

            self.addTrackingArea(area)
            self.trackingMoved = area
        }
    }
    
    open override func layout(){
        super.layout()
        print("layout")
        
        if let area = self.trackingEnteredAndExited {
            
            self.removeTrackingArea(area)
            self.addMonitorWithCase(.enteredAndExited)
        }
        
        if let area = self.trackingMoved {
            self.removeTrackingArea(area)
            self.addMonitorWithCase(.moved)
        }
    }
    
    open override func mouseEntered(with event: NSEvent)  {
        super.mouseEntered(with: event)
        print("mouseEntered \(self.description)")
        self.mouseMoveEntered()
    }
    
    open override func mouseExited(with event: NSEvent)  {
        super.mouseExited(with: event)
        print("mouseExited \(self.description)")
        self.mouseMoveExited()
    }
    
    open override func mouseMoved(with event: NSEvent) {
        super.mouseMoved(with: event)
        print("mouseMoved \(self.description)")

    }

    //MARK: -   mouse Responder

    open override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        print("mouseDown")
    }
    
    open override func mouseUp(with event: NSEvent) {
        super.mouseUp(with: event)
        print("mouseUp")
    }
    
    open override func rightMouseDown(with event: NSEvent) {
        super.rightMouseDown(with: event)
        print("rightMouseDown")
      
        var pt = self.convert(event.locationInWindow, to: self)
        
        print("x=\(pt.x),y=\(pt.y)")
        
        print("window flipped = \(event.window!.contentView!.isFlipped)")

        print("self flipped = \(self.isFlipped)")

        
        if self.isFlipped == true, let bounds = event.window?.contentView?.bounds{
            pt.y = bounds.size.height - pt.y
            
            print("x=\(pt.x),y=\(pt.y)")
        }
    }
   
    open override func rightMouseUp(with event: NSEvent) {
        super.rightMouseUp(with: event)
        print("rightMouseUp")
    }
    
    //MARK: -   release
    
    open func removeAllMouseEvent(){
        
        if let area = self.trackingEnteredAndExited {
            
            self.removeTrackingArea(area)
            self.trackingEnteredAndExited = nil
        }
        
        if let area = self.trackingMoved {
            self.removeTrackingArea(area)
            self.trackingMoved = nil
        }
    }
    
    deinit {
        
       removeAllMouseEvent()
    }
    
}
